package com.beesphere.excel.reader.tests;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.beesphere.excel.reader.CellFormatter;
import com.beesphere.excel.reader.CellType;

public class DateCellFormatter implements CellFormatter {
	
	private static final long serialVersionUID = -6903321898488303501L;

	private static final String DEFAULT = "yyyy-MM-dd";
	
	private Locale locale;
	private String format = DEFAULT;

	@Override
	public Object format (Object value, CellType type) {
		if (CellType.DATE.equals(type)) {
			return toString ((Date)value, format, locale);
		}
		return value;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	private String toString (Date date, String format, Locale locale) {
        SimpleDateFormat formatter = null;
        if (locale != null) {
        	formatter = new SimpleDateFormat(format, locale);
        } else {
        	formatter = new SimpleDateFormat(format);
        }
        return formatter.format(date);
    }

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}	
	
}
